<?php

class Storage_Adapter_Local extends Storage_Adapter
{
	protected $_basePath;

	protected $_baseUrl;

	public function __construct($id, $config)
	{
		$this -> _id = $id;
		$this -> _baseUrl = '/public/';
		$this -> _basePath = APP_ROOT_PATH . DIRECTORY_SEPARATOR;
	}

	function getContents($relative_path, $force = TRUE)
	{
		$path = $this -> mapPath($relative_path);
		if (file_exists($file))
		{
			return @file_get_contents($path);
		}
		throw new Storage_Exception('File {file} does not exists!', array('{file}' => $path));
	}

	function putContens($relative_path, $data, $force = TRUE)
	{

	}

	function get($relative_path, $local_file, $force = TRUE)
	{
		self::_makeLocalDir(dirname($local_file));
		$path = $this -> mapPath($relative_path);
		return self::_copyLocalFile($path, $local_file);
	}

	function put($relative_path, $local_file, $force = TRUE)
	{
		if (!is_file($local_file))
		{
			if ($force)
			{
				throw new Storage_Exception('file "{file}" does not exists.', array('{file}' => $local_file));
			}
			return FALSE;
		}
		
		$path = $this -> mapPath($relative_path);

		$this -> makeDir(dirname($relative_path), 0777);

		if (!@copy($local_file, $path))
		{
			if ($force)
			{
				throw new Storage_Exception('could not copy {from} to {to}', array(
					'{from}' => $path,
					'{to}' => $local_file
				));
			}
			return FALSE;
		}

		@chmod($path, 0777);

		return TRUE;
	}

	function putContents($relative_path, $data, $force = TRUE)
	{
		$path = $this -> mapPath($relative_path);
		$this -> makeDir(dirname($relative_path));
		return self::_writeLocalFile($path, $data);
	}

	function removeDir($relative_path)
	{
		return rmdir($this -> mapPath($relative_path));

	}

	function delete($relative_path, $force = TRUE)
	{
		$path = $this -> mapPath($relative_path);

		if (!file_exists($path))
		{
			if ($force)
			{
				throw new Storage_Exception('File {file} does not exists!', array('{file}' => $path));
			}
			return FALSE;
		}
		if (is_writable($path))
		{
			if (!@unlink($storage_path))
			{
				if ($force)
				{
					throw new Storage_Exception('File {file} could not be removed!', array('{file}' => $path));
				}
				return FALSE;
			}
		}
		return TRUE;
	}

	function isFile($relative_path)
	{
		return @file_exists($this -> mapPath($relative_path));
	}

	function isDir($relative_path)
	{
		$path = $this -> mapPath($relative_path);
		return is_dir($path);
	}

	function makeDir($relative_path, $mode = 0777)
	{
		$path = $this -> mapPath($relative_path);

		if (is_dir($path))
		{
			return TRUE;
		}

		$path = $this -> mapPath($relative_path);

		$parts = array_filter(explode('/', $path));

		$pPath = array_shift($parts);

		while (count($parts) > 0)
		{
			$pPath .= '/' . array_shift($parts);

			// If it doesn't exist, create it
			if (!is_dir($pPath))
			{
				if (!@mkdir($pPath))
				{
					throw new Storage_Exception('Unable to make directory "{dir}"', array('{dir}' => $pPath));
				}
				@chmod($pPath, $mode);
			}
		}

		if (!is_dir($path))
		{
			throw new Storage_Exception('Unable to make directory "{dir}"', array('dir' => $path));
		}
		return TRUE;

	}

}
